﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Diagnostics;

namespace LibIpMsgNet
{

    public delegate void TMessageReceivedHandler(object sender, EventArgs e);

    public class TIpMessenger
    {
        private TConnection conn;
        //private MessageManager msgmng;

        public event TMessageReceivedHandler MessageReceived;

        private System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();


        public TIpMessenger()
        {
            IPEndPoint ep = new IPEndPoint
                (Dns.GetHostEntry(Environment.MachineName).AddressList[0], 
                (int)IPMSG_DEF.IPMSG_DEFAULT_PORT);

            conn = new TConnection(ep);
            conn.UdpReceived += new TUdpReceivedHandle(conn_UdpReceived);

            BroadcastEntry(IPMSG_DEF.IPMSG_BR_ENTRY);

            timer.Interval = 1000;
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();

           // msgmng = new MessageManager();
        }

        private void conn_UdpReceived(object sender, TUdpReceivedArgs args)
        {

            TMessageBuffer msg = new TMessageBuffer();

            TRecvBuffer buf = args.Buffer;

            if (buf!=null && buf.Size>0)
                if (TMessageManager.ResolveMessage(buf, ref msg))
                {

                    Trace.WriteLine(String.Format("Command: 0x{0:X}, Option: 0x{1:X}", 
                        IPMSG_DEF.GET_MODE(msg.Command), 
                        IPMSG_DEF.GET_OPT(msg.Command)));


                    switch (IPMSG_DEF.GET_MODE(msg.Command))
                    {
                        case IPMSG_DEF.IPMSG_BR_ENTRY:
                            break;

                        case IPMSG_DEF.IPMSG_BR_EXIT:
                            break;

                        case IPMSG_DEF.IPMSG_ANSENTRY:
                            break;

                        case IPMSG_DEF.IPMSG_BR_ABSENCE:
                            break;

                        case IPMSG_DEF.IPMSG_SENDMSG:
                            if (MessageReceived != null)
                                MessageReceived(msg, new EventArgs());
                            break;

                        case IPMSG_DEF.IPMSG_RECVMSG:
                            break;

                        case IPMSG_DEF.IPMSG_READMSG: 
                        case IPMSG_DEF.IPMSG_ANSREADMSG:
                            break;

                        case IPMSG_DEF.IPMSG_BR_ISGETLIST2:
                            break;

                        case IPMSG_DEF.IPMSG_OKGETLIST:
                            break;

                        case IPMSG_DEF.IPMSG_GETLIST:
                            break;

                        case IPMSG_DEF.IPMSG_ANSLIST:
                            break;

                        case IPMSG_DEF.IPMSG_GETINFO:
                            //MsgGetInfo(&msg);
                            break;

                        case IPMSG_DEF.IPMSG_SENDINFO:
                            //MsgSendInfo(&msg);
                            break;

                        case IPMSG_DEF.IPMSG_GETPUBKEY:
                            MsgGetPubKey(msg);
                            break;

                        case IPMSG_DEF.IPMSG_ANSPUBKEY:
                            //MsgAnsPubKey(&msg);
                            break;

                        case IPMSG_DEF.IPMSG_GETABSENCEINFO:
                            //MsgGetAbsenceInfo(&msg);
                            break;

                        case IPMSG_DEF.IPMSG_SENDABSENCEINFO:
                            //MsgSendAbsenceInfo(&msg);
                            break;

                        case IPMSG_DEF.IPMSG_RELEASEFILES:
                            //MsgReleaseFiles(&msg);
                            break;
                    }
                }


        }

        public void SendMessage(IPEndPoint EndPoint, ulong Command, string Text, string ExText)
        {
            byte[] buf;
            int len;

            TMessageManager.MakeMessage(out buf, conn.MakePacketNo(), Command, Text, ExText, out len, conn.LocalHost);

            if (len > 0)
            {
                conn.UdpSend(EndPoint, buf, len);
            }
        }

        public void SendMessage(IPEndPoint EndPoint, string Text)
        {
            SendMessage(EndPoint,(int)IPMSG_DEF.IPMSG_SENDMSG, Text, "");
        }

        private void BroadcastEntry(ulong Command)
        {
            SendMessage(new IPEndPoint(IPAddress.Any, (int)IPMSG_DEF.IPMSG_DEFAULT_PORT), 
                Command, "aa", "");
        }

        private void SendNoOperate(IPEndPoint EndPoint)
        {
            SendMessage(EndPoint, (int)IPMSG_DEF.IPMSG_NOOPERATION, "", "");
        }

        #region udp message response
        //---------------

        private void MsgGetPubKey(TMessageBuffer msg)
        {
            SendMessage(msg.Host.EndPoint, IPMSG_DEF.IPMSG_ANSPUBKEY, "", "");
        }

        //---------------        
        #endregion

        private void timer_Tick(object sender, EventArgs e)
        {
            conn.UdpRecv();
        }


    }
}
